/**
 * 
 */
package inz.model.classification;

import inz.common.LoggingModule;
import inz.model.patterns.DbSequence;
import inz.model.patterns.Sequence;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/** Represents example of document */
public class Example
{
	/** loger */
	private static Logger logger = Logger.getLogger(LoggingModule.CLASSIFICATION_LOGGER);
	/** map of binary attributes (sequence - does it exist in this example) */
	private Map<Sequence, Boolean> attributes = new LinkedHashMap<Sequence, Boolean>();
	/** simple representation of example */
	private Set<Integer> attrs = new LinkedHashSet<Integer>();



	/** Constructor for test examples. Fills attributes map.
	 * @param frequentSequences sequences that will be attributes of the example
	 * @param dbSequence document represented as database sequence
	 * @param maxDist maximal distance between elements of sequence */
	public Example(Set<Sequence> frequentSequences, DbSequence dbSequence, int maxDist)
	{
		int number = 1;
		for (Sequence seq : frequentSequences)
		{
			boolean contains = dbSequence.contains(seq, maxDist);
			attributes.put(seq, contains);
			if (contains)
			{
				attrs.add(number);
			}
			number++;
		}
	}

	/** Checks the value of given attribute in the example (weather example contains given sequence)
	 * @param seq sequence to be contained by example
	 * @return true if example contains sequence */
	public boolean getAttrVal(Sequence seq)
	{
		return attributes.get(seq);
	}

	/** Checks if set of positive attributes of this example contains given set
	 * @param set
	 * @return */
	public boolean contains(Set<Integer> set)
	{
		return attrs.containsAll(set);
	}

	/** @return simple representation of example (numbers of contained sequences) */
	public Set<Integer> getExampleSet()
	{
		Set<Integer> resultSet = new HashSet<Integer>();
		int k = 1;
		for (Map.Entry<Sequence, Boolean> entry : attributes.entrySet())
		{
			if (entry.getValue())
			{
				resultSet.add(k);
			}
			k++;
		}
		return resultSet;
	}

	public void print()
	{
		logger.log(Level.INFO, "[EXAMPLE] attributes: " + attributes, "SpecificExamples");
		logger.log(Level.INFO, "[/example] simple: " + getExampleSet(), "examples");
	}
	@Override
	public String toString()
	{
		// String result = "[EXAMPLE] attributes: " + attributes + "\n";
		String result = "[example] simple: " + getExampleSet();
		return result;
	}

}
